//
//  AppDelegate.m
//  Thinkape-iOS
//
//  Created by tixa on 15/4/7.
//  Copyright (c) 2015年 TIXA. All rights reserved.
//

#import "AppDelegate.h"
#import "JPUSHService.h"
#import "IQKeyboardManager.h"
#import "LogineViewController.h"
#import "SubmitApproveViewController.h"
//#import "AppDelegate+YLJPushSDK.h"

@interface AppDelegate ()

@end

#define JPushSDK_AppKey @"d9cbb94a99ce934d30fc9e78"
#define isProduction     NO

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    self.window.backgroundColor =[UIColor clearColor];

    [IQKeyboardManager sharedManager].enable = YES;

    [IQKeyboardManager sharedManager].shouldResignOnTouchOutside = YES;
    NSString *filePath = [NSString stringWithFormat:@"%@/account",NSHomeDirectory()];

    if (![DataManager shareManager].webDomain) {
        [DataManager shareManager].webDomain = @"http://27.115.23.126:5032";
   }
    NSLog(@"path :%@",filePath);
//嘻嘻嘻哈哈哈
    
//    if (SYSTEM_VERSION_GREATER_THAN(@"7.0") && SYSTEM_VERSION_LESS_THAN(@"8.0")) {
//        [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor, nil]];
//        NSLog(@"text attri : 7.0");
//    }
//    else if (SYSTEM_VERSION_GREATER_THAN(@"8.0")){
//        [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName, nil]];
//        NSLog(@"text : 8.0");
//        
//    }
    
//    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navi_bg_ios7@2x"]  forBarMetrics:UIBarMetricsDefault];
    
    //通过代码创建动态shortcutItems
//    [self createShortCutItems];
    
 
    
    // Required
    if (SYSTEM_VERSION_GREATER_THAN(@"8.0")) {
        //可以添加自定义categories
        [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |UIUserNotificationTypeSound |UIUserNotificationTypeAlert)categories:nil];
    } else {
        //categories 必须为nil
        [JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |UIRemoteNotificationTypeSound |UIRemoteNotificationTypeAlert)categories:nil];
    }
    
    [JPUSHService setupWithOption:launchOptions appKey:JPushSDK_AppKey channel:channel apsForProduction:isProduction];
    //isProduction这个值生产环境为YES，开发环境为NO(BOOL值)];

    
    
    if (launchOptions[@"UIApplicationLaunchOptionsShortcutItemKey"] == nil) {
        return YES;
    }else{
        return NO;
    }

    
    
    return YES;
}


//创建shortItems
- (void) createShortCutItems
{
    NSMutableArray *shortcutItems = [NSMutableArray array];
    UIApplicationShortcutItem *item0 = [[UIApplicationShortcutItem alloc]initWithType:@"1" localizedTitle:@"随手报"];
    UIApplicationShortcutItem *item1 = [[UIApplicationShortcutItem alloc]initWithType:@"2" localizedTitle:@"么么哒"];
    [shortcutItems addObject:item0];
    [shortcutItems addObject:item1];
    [[UIApplication sharedApplication] setShortcutItems:shortcutItems];
    
}


-(void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"Notice3DTouch" object:self userInfo:@{@"type":shortcutItem.type}];

    
}


-(void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
     
}


#pragma mark -- 推送
//注册设备
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    AccountModel *model = [DataManager shareManager].account;
    
    //这个方法是设置别名和tag 可省
     [JPUSHService setTags:[NSSet setWithObject:model.Uname] alias:model.Uname fetchCompletionHandle:^(int iResCode, NSSet *iTags, NSString *iAlias) {
            NSLog(@"rescode: %d, \ntags: %@, \nalias: %@\n", iResCode, iTags , iAlias);
       }];
    // Required
    [JPUSHService registerDeviceToken:deviceToken];
}


//（App即将进入前台）中将小红点清除
- (void)applicationWillEnterForeground:(UIApplication *)application {
    NSLog(@"进入前台");
    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
}


//处理推送错误：
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    //Optional
    NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
}


#pragma mark 修改通知处理函数
//App在后台时收到推送时的处理
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(nonnull void (^)(UIBackgroundFetchResult))completionHandler
{
    //推送消息携带 content-available: 1 是Background 运行的必须参数，如果不携带此字段则与iOS7 之前版本的普通推送一样。
    //清空角标
    [JPUSHService resetBadge];
    if (!userInfo) {
        completionHandler(UIBackgroundFetchResultNoData);
        return ;
    }
    completionHandler(UIBackgroundFetchResultNewData);
    [JPUSHService handleRemoteNotification:userInfo];
    completionHandler(UIBackgroundFetchResultNewData);
    
    //根据服务器设置的 userInfo 的格式，进行解析

}


//- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window
//{
//    return UIInterfaceOrientationMaskAllButUpsideDown;
//    
//    
//}

// Required
//-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
//    [JPUSHService registerDeviceToken:deviceToken];
//}

// Required
//-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
//    [JPUSHService handleRemoteNotification:userInfo];
//    application.applicationIconBadgeNumber = 0;
//}

//- (void)application:(UIApplication *)application
//didReceiveLocalNotification:(UILocalNotification *)notification {
//    [JPUSHService showLocalNotificationAtFront:notification identifierKey:nil];
//}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    [application setApplicationIconBadgeNumber:0];
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}




-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    return UIInterfaceOrientationMaskAllButUpsideDown;
    
}

// 支持设备自动旋转
-(BOOL)shouldAutorotate
{
    return YES;
}


@end
